package com.clkj.goldpartner.filter;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

@Configuration
@EnableSwagger2
public class AuthConfig implements WebMvcConfigurer {
    @Resource
    private AuthInterceptor authInterceptor;

    //全局跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {

        // registry.addMapping("/**") //添加映射路径
//                .allowedOrigins("http://192.168.0.112:9001/") //放行哪些原始域
//                .allowedMethods("*") //放行哪些原始域(请求方式) //"GET","POST", "PUT", "DELETE", "OPTIONS"
//                .allowedHeaders("*") //放行哪些原始域(头部信息)
//                .allowCredentials(true) //是否发送Cookie信息
////		        .exposedHeaders("access-control-allow-headers",
////		        				"access-control-allow-methods",
////		        				"access-control-allow-origin",
////		        				"access-control-max-age",
////		        				"X-Frame-Options") //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
//                .maxAge(1800);
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "DELETE")
                .allowedHeaders("*")
                .exposedHeaders("Access-Control-Allow-Origin")
                .allowCredentials(true);

    }

    /**
     * 添加拦截器配置
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //其中/**表示对所有资源的拦截---excludePathPatterns("/login")表示login进行放行
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/system/login")
                .excludePathPatterns("/goods/exportGoodsTotal")
                .excludePathPatterns("/goods/exportlistGyGoodsTotal")
                .excludePathPatterns("/goods/exportlistGoodsSaleTotal")
                .excludePathPatterns("/goods/exportGoodsList")
                .excludePathPatterns("/stock/exportGoodsInDetail")
                .excludePathPatterns("/stock/exportStock")
                .excludePathPatterns("/stock/exportGYSDetail")
                .excludePathPatterns("/stock/exportlistGoodsSaleDetail")
                .excludePathPatterns("/stock/exportListStockWarning")
                .excludePathPatterns("/stock/exportGoodsStockWarn")
                .excludePathPatterns("/system/exportCustomerWarn")
                .excludePathPatterns("/system/exportSupplierWarn")
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/error")
                .excludePathPatterns("/webjars/**");

//        //如果有多个拦截器，都在这里进行拦截器的添加
//        registry.addInterceptor(authInterceptor).addPathPatterns("/**")
//                .excludePathPatterns("/login").excludePathPatterns("/assets/**");
    }

}
